package tr.helper.controller.user;

import cn.hutool.core.collection.CollectionUtil;
import tr.helper.CommonUtil;
import tr.helper.controller.Context;
import tr.helper.controller.SecurityController;
import tr.helper.controller.home.HomeController;
import tr.helper.controller.home.HomeMain;
import tr.helper.domain.DataConfig;
import tr.helper.repository.table.User;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.util.Pair;
import javafx.util.StringConverter;
import lombok.var;
import org.apache.commons.lang3.StringUtils;

public class UserAddController extends SecurityController {

    @FXML
    private TextField username;

    @FXML
    private TextField password;

    @FXML
    public ChoiceBox<Pair<String, Long>> boxChoice;

    @FXML
    public ChoiceBox<Pair<String, Long>> boxUsers;

    @Override
    public void init(URL location, ResourceBundle resources) {
        DataConfig.initMasters(boxChoice);
        bindBoxUsers();
    }

    public void bindBoxUsers() {
        List<User> users = userDao.selectAll(null);
        if (CollectionUtil.isNotEmpty(users)) {
            List<Pair<String, Long>> selectors = users.stream().map(u -> new Pair<>(u.getUsername(), u.getId())).collect(Collectors.toList());
            boxUsers.setConverter(new StringConverter<Pair<String, Long>>() {
                @Override
                public String toString(Pair<String, Long> pair) {
                    return pair.getKey();
                }

                @Override
                public Pair<String, Long> fromString(String string) {
                    return null;
                }
            });
            boxUsers.getItems().addAll(selectors);
            boxUsers.setValue(DataConfig.EMPTY_PAIR);
        }
    }

    @FXML
    public void doAdd() {
        if (StringUtils.isBlank(username.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "用户名不能为空");
            return;
        }
        if (StringUtils.isBlank(password.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "密码不能为空");
            return;
        }
        final var selectedItem = boxUsers.getSelectionModel().getSelectedItem();
        userDao.insertSelective(User.builder().id(CommonUtil.nextId()).username(username.getText()).password(password.getText())
            .master(boxChoice.getValue().getKey()).parentId(null == selectedItem ? 0L : boxUsers.getSelectionModel().getSelectedItem().getValue()).build());

        HomeController homeController = (HomeController) Context.controllers.get(HomeController.class.getSimpleName());
        if (null != homeController) {
            //homeController.userStage.close();
        }
        CommonUtil.alert(null, "保存成功");
    }

    @FXML
    public void doClear() {
        username.setText(StringUtils.EMPTY);
        password.setText(StringUtils.EMPTY);
    }

}
